﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PJAA_Inventory_and_AR___Prototype
{
    public partial class Complete_Transaction : Form
    {
        private SQLMaster master;
        private Main_Menu MM;

        public Complete_Transaction(Main_Menu MM)
        {
            //added this block to make the form transparent
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            InitializeComponent();

            this.MM = MM;
            master = new SQLMaster();
            loadAllUnpaid();
        }

        private void loadAllUnpaid()
        {
            List<object[]> ArrList = new List<object[]>();
            ArrList = master.loadUnpaid();

            IssueInv_DGV.Rows.Clear();//clear all rows of nay value

            foreach (object[] strArray in ArrList)
            {
                if (IssueInv_Name_TxtBox.Text != string.Empty)
                {
                    if ((strArray[3] as string).ToLower().Contains(IssueInv_Name_TxtBox.Text.ToLower()))
                    {//if it contains the string in the seach bar, taht row wil be added to the data grid view
                        IssueInv_DGV.Rows.Add(strArray);
                    }
                }
                else
                {
                    IssueInv_DGV.Rows.Add(strArray);
                }
            }

        }

        public string _ClName
        {
            get { return IssueInv_DGV.SelectedCells[3].Value.ToString(); }
        }
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void CompleteTrans_Cancel_Btn_Click(object sender, EventArgs e)
        {

        }

        private void IssueInv_DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 6)
            { //if cell clicked is 6
                
                if (IssueInv_DGV.SelectedCells[0].Value == null)
                { MessageBox.Show("Please select a customer first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                else
                {
                    Complete_Transaction_button compTransaction = new Complete_Transaction_button(IssueInv_DGV.SelectedCells[2].Value.ToString(), IssueInv_DGV.SelectedCells[5].Value.ToString());//calls complete transaction
                    compTransaction.TopLevel = false; //the window will not be top level 
                    compTransaction.Dock = DockStyle.Fill; //when loaded it will fill the panel
                    panel1.Controls.Clear();//clear all elements in current panel
                    panel1.Controls.Add(compTransaction);//add the window in the panel
                    compTransaction.Show();//shows the window
                    MM.Clientlbl.Visible = true;
                    MM.label_client.Visible = true;
                    MM._ClientName = _ClName;//calls the _clname string so that mainmenu's clname will be set
                }
            }
        }

        private void IssueInv_Name_TxtBox_TextChanged(object sender, EventArgs e)
        {
            loadAllUnpaid();
        }

        private void IssueInv_Close_Btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
